﻿using EasyLinkerSDK.Client.Interface;
using EasyLinkerSDK.Client.MQTT;
using EasyLinkerSDK.Client.TCP;
using EasyLinkerSDK.Enum;
using EasyLinkerSDK.Model.Client.Common.Interface;
using EasyLinkerSDK.Model.Info;
using EasyLinkerSDK.Model.Server.Common.Interface;
using EasyLinkerSDK.Model.User;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using static EasyLinkerSDK.Client.Interface.EventHandler;

namespace EasyLinkerSDK
{
    public class EasyLinkerClient
    {
        private IELClient _client = null;

        /// <summary>
        /// 消息到达委托
        /// </summary>
        /// <param name="message"></param>
        public event MessageReceivedEventHandler DoMessageReceived;

        public event ClientLogEventHandler DoClientLog;

        /// <summary>
        /// 是否连接到服务器
        /// </summary>
        public bool IsConnected
        {
            get
            {
                if (_client == null)
                {
                    return false;
                }
                else
                {
                    return _client.IsConnected;
                }
            }
        }

        public EasyLinkerClient(ClientInfo clientInfo, ProtocolType protocol)
        {
            if (clientInfo == null
                || string.IsNullOrEmpty(clientInfo.DeviceId)
                || string.IsNullOrEmpty(clientInfo.UserId)
                || string.IsNullOrEmpty(clientInfo.Key)
                || string.IsNullOrEmpty(clientInfo.Server))
            {
                throw new NullReferenceException("Param can not null");
            }
            if (protocol != ProtocolType.Tcp && protocol != ProtocolType.Mqtt && protocol != ProtocolType.Http)
            {
                throw new Exception("Unsupport protocol type");
            }
            switch (protocol)
            {
                case ProtocolType.Tcp:
                    _client = new TCPClient(clientInfo);
                    break;
                case ProtocolType.Mqtt:
                    _client = new MQTTClient(clientInfo);
                    break;
                case ProtocolType.Http:
                    break;
                case ProtocolType.Udp:
                    break;
                default:
                    throw new Exception("Unsupport protocol type");
            }
            if (_client != null)
            {
                _client.DoMessageReceived += MessageReceived;
                _client.DoClientLog += ClientLog;
            }
        }

        public bool Start()
        {
            try
            {
                return _client.Start().Result;
            }
            catch (Exception ex)
            {
                ClientLog($"Connect failed, please check your userid or password.", ex);
                return false;
            }
        }

        public bool Stop()
        {
            try
            {
                if(_client == null || !_client.IsConnected)
                {
                    return true;
                }
                return _client.Stop().Result;
            }
            catch (Exception ex)
            {
                ClientLog($"Can not stop easylinker client.", ex);
                return false;
            }
        }

        public void Send(IClientMessage message)
        {
            if (_client != null)
            {
                _client.Send(message);
            }
        }

        public SdkInfo SdkInfo()
        {
            return new SdkInfo();
        }

        private void MessageReceived(IServerMessage message)
        {
            DoMessageReceived?.Invoke(message);
        }

        private void ClientLog(object sender, Exception ex)
        {
            DoClientLog?.Invoke(sender, ex);
        }
    }
}
